import {SysNewsAdd, SysNewsData, SysNewsDetail, SysNewsParams} from "@/pages/news/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";
import {HttpResult, Page} from "@/services/typings";


export const sysNewsPageList = async (sysNewsParams: SysNewsParams) => {
  return request<HttpResult<Page<SysNewsData>>>('/api/news/list', {
    method: 'POST',
    data: sysNewsParams
  });
}


export const sysNewsUpdate = async (sysNewsAdd: SysNewsAdd) => {
  return request<HttpResult<string>>('/api/news/update', {
    method: 'POST',
    data: sysNewsAdd
  });
}

export const sysNewsAdd = async (sysNewsAdd: SysNewsAdd) => {
  return request<HttpResult<string>>('/api/news/add', {
    method: 'POST',
    data: sysNewsAdd
  });
}

export const sysNewsRemove = async ( ids: (number | string)[] ) => {
  return request<HttpResult<string>>('/api/news/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysNewsDetail = async (id: number) => {
  return request<HttpResult<SysNewsDetail>>('/api/news/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const sysNewsImport = async (formData: FormData) => {
  return request<HttpResult<string>>('/api/news/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysNewsExport = async (ids: (number | string)[]) => {
  return request<HttpResult<string>>('/api/news/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysNewsExport = async (ids: (number | string)[]) => {
  await sysNewsExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const sysNewsExportTemplate = async () => {
  return request<HttpResult<string>>('/api/news/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysNewsExportTemplate = async () => {
  await sysNewsExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}
